<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click.native="showRoleDialog"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="setList">
          <el-table-column label="序号" width="120" type="index" :index="updataIndex" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small" @click="clickShowAssign(row.id)">分配权限</el-button>
              <el-button size="small" @click="edit(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="query.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="query.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <!-- 分页组件 -->
        <!-- <el-row type="flex" justify="center" align="middle" style="height: 60px"> -->
        <!-- 分页组件 -->
        <!-- <el-pagination layout="prev,pager,next" /> -->
        <!-- </el-row> -->
      </el-card>
    </div>
    <!-- 新增角色 -->
    <el-dialog
      :title="dialogTitle"
      :visible="roleDialogVisible"
      width="50%"
      @close="closeRoleDialog"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirmAdd">确定</el-button>
      </el-row>
    </el-dialog>
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closeshowAssignDialog"
      @open="getPermissionlist"
    >
      <!-- 权限点数据展示 -->
      <el-tree
        ref="tree"
        :data="Permissionlist"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="per">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { getSetlist, postSetrole, deleteSetrole, getSetrole, putSetrole, putSetassignPrem } from '@/api/Setting'
import { getPermissionlist } from '@/api/Permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    return {
      setList: [],
      query: {
        page: 1,
        pagesize: 20
      },
      total: 0,
      roleDialogVisible: false,
      roleForm: {
        name: '',
        description: '',
        id: '',
        permIds: []
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      dialogTitle: '',
      showAssignDialog: false,
      Permissionlist: [],
      cid: null,
      permIds: []
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const res = await getSetlist(this.query)
      this.setList = res.rows
      this.total = res.total
    },
    // 翻页
    handleSizeChange(val) {
      this.query.pagesize = val
      this.query.page = 1
      this.getlist()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getlist()
    },
    // 打开弹框
    showRoleDialog() {
      this.dialogTitle = '新增角色'
      this.roleDialogVisible = true
    },
    // 取消按钮
    cancel() {
      this.roleDialogVisible = false
      this.$refs.roleForm.resetFields()
    },
    // 关闭弹框
    closeRoleDialog() {
      this.roleDialogVisible = false
      this.$refs.roleForm.resetFields()
    },
    confirmAdd() {
      // 兜底校验
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          // todo login
          if (this.roleForm.id) {
            await putSetrole(this.roleForm)
          } else {
            await postSetrole(this.roleForm)
          }
          this.$message.success(`${this.roleForm.id ? '编辑' : '新增'}成功`)
          this.$refs.roleForm.resetFields()
          this.roleDialogVisible = false
          this.getlist()
          this.reset()
        }
      })
    },
    // 删除按钮
    del(id) {
      this.$confirm('确认删除吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteSetrole(id)
        this.query.page = 1
        this.getlist()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑按钮
    async edit(id) {
      this.dialogTitle = '编辑角色'
      this.roleDialogVisible = true
      const res = await getSetrole(id)
      this.roleForm = res
    },
    reset() {
      this.roleForm = {
        name: '',
        description: '',
        id: '',
        permIds: []
      }
    },
    // updataIndex(val) {
    //   return val * 2
    // },
    updataIndex(val) {
      return (this.query.page - 1) * (this.query.pagesize) + (val + 1)
    },
    // 打开按钮
    clickShowAssign(id) {
      this.showAssignDialog = true
      this.cid = id
    },
    // close方法
    closeshowAssignDialog() {
      this.showAssignDialog = false
      this.$refs.tree.setCheckedKeys([])
    },
    // open里的方法
    async getPermissionlist() {
      const res = await getPermissionlist()
      this.Permissionlist = transTree(res)
      const Detail = await getSetrole(this.cid)
      this.$refs.tree.setCheckedKeys(Detail.permIds)
    },
    // 确认按钮
    async per() {
      await putSetassignPrem({
        id: this.cid,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    },
    // 取消按钮
    closeAssignDialog() {
      this.$refs.tree.setCheckedKeys([])
      this.showAssignDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 15px;
}
</style>
